package com.example.administrator.nurse.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import com.example.administrator.nurse.R;

/**
 * Created by Administrator on 2016/4/14.
 */
public class CircleIndicator extends View {

    public static final int TORECT=0;
    public static final int TOPOINT=1;
    public static final int POINT=0;
    public static final int RECT=1;
    private GradientDrawable drawable;
    private int RectSize;
    private int PointSize;
    private float cornerRadius;
    private int state;
    private MorphingAnimation RectAnimator;
    private MorphingAnimation PointAnimator;

    public CircleIndicator(Context context) {
        super(context);
        init();
    }

    public CircleIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private  void init(){
        cornerRadius=getContext().getResources().getDimension(R.dimen.indication_corner_radius);
        drawable=new GradientDrawable();
        PointSize= (int) getContext().getResources().getDimension(R.dimen.indicating_size);
        drawable.setSize(PointSize, PointSize);
        RectSize= (int) getContext().getResources().getDimension(R.dimen.indicating_rect);
        int stroke= (int) getContext().getResources().getDimension(R.dimen.indication_stroke);
        drawable.setStroke(stroke, getContext().getResources().getColor(R.color.nav_color));
        drawable.setCornerRadius(cornerRadius);
        if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN){
            this.setBackground(drawable);
        }else{
            this.setBackgroundDrawable(drawable);
        }
        setState(POINT);
    }

    public void setToState(int ToState){
        switch (ToState){
            case TOPOINT:
                if (state==RECT){
                    MorphingToPoint(RectSize,PointSize,cornerRadius,cornerRadius,getResources().getColor(R.color.nav_color),getResources().getColor(R.color.arg_white));
                    setState(POINT);
                }
                break;
            case TORECT:
                if (state==POINT){
                    MorphingToRect(PointSize,RectSize,cornerRadius,cornerRadius,getResources().getColor(R.color.arg_white),getResources().getColor(R.color.nav_color));
                    setState(RECT);
                }
                break;
        }
    }
    private void MorphingToPoint(int fromWidth,int toWidth,float fromCornerRadius,float toCornerRadius,int fromColor,int toColor){
        if(PointAnimator==null){
            PointAnimator=new MorphingAnimation(this,drawable);
        }
        PointAnimator.setmFromCornerRadius(fromCornerRadius);
        PointAnimator.setmToCornerRadius(toCornerRadius);
        PointAnimator.setmFromWidth(fromWidth);
        PointAnimator.setmToWidth(toWidth);
        PointAnimator.setmFromColor(fromColor);
        PointAnimator.setmToColor(toColor);
        PointAnimator.setmDuration(300);
        PointAnimator.start();
    }
    private void MorphingToRect(int fromWidth,int toWidth,float fromCornerRadius,float toCornerRadius,int fromColor,int toColor){
        if (RectAnimator==null){
            RectAnimator=new MorphingAnimation(this,drawable);
        }
//        MorphingAnimation animation=new MorphingAnimation(this,drawable);
        RectAnimator.setmFromCornerRadius(fromCornerRadius);
        RectAnimator.setmToCornerRadius(toCornerRadius);
        RectAnimator.setmFromWidth(fromWidth);
        RectAnimator.setmToWidth(toWidth);
        RectAnimator.setmFromColor(fromColor);
        RectAnimator.setmToColor(toColor);
        RectAnimator.setmDuration(1000);
        RectAnimator.start();
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(PointSize,PointSize);
    }

    public void setState(int state) {
        this.state = state;
    }
    private void Morphing(int fromWidth,int toWidth,float fromCornerRadius,float toCornerRadius,int fromColor,int toColor){
        MorphingAnimation animation=new MorphingAnimation(this,drawable);
        animation.setmFromCornerRadius(fromCornerRadius);
        animation.setmToCornerRadius(toCornerRadius);
        animation.setmFromWidth(fromWidth);
        animation.setmToWidth(toWidth);
        animation.setmFromColor(fromColor);
        animation.setmToColor(toColor);
        animation.setmDuration(1000);
        animation.start();
    }
}
